package gdma.hqdes.bean;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wangyue
 * @Version 3.0
 * @Date 2024-08-01
 */

@Data
public class YearStat extends HqdProject implements Stat {

    @TableField(exist = false)
    List<UnitScore> scores;


    @Override
    public String toShortString() {
        return String.valueOf(year);
    }

    @JsonIgnore
    public Map<Long, UnitScore> getScoreMap() {
        if(scores == null) return null;
        return scores.stream()
                //得分按指标分组成map，如果包含多个单位则计算得分的平均值，已符合按地区同时的需求要求
                .collect(Collectors.groupingBy(UnitScore::getIndexId, Collectors.averagingDouble(UnitScore::getScore))).entrySet().stream()
                .collect(Collectors.toMap(e -> e.getKey(), e -> {
                    UnitScore unitScore = new UnitScore();
                    unitScore.setIndexId(e.getKey());
                    unitScore.setScore(e.getValue().floatValue());
                    return unitScore;
                }));
    }

}
